<?php
/* by yechen
 * 2020-04-28
 * 云收款加解密demo
 * */
header("Content-type: text/html; charset=utf-8");
class webApp{
    #==============此为fat7测试环境参数,生产参数和请求地址请自行参考文档替换===========
    const DEBUG			    =false;
    #获取token地址,生产需替换
    const token             ='https://my-st1.orangebank.com.cn:567/fat7/openapi/gateway/as/token.oauth2';

    #接口公共请求地址,生产需替换
    const baseUrl           ='https://my-st1.orangebank.com.cn:567/fat7/openapi/gateway';

    #APPID ,生产需替换
    const appid             = 'd5f8ee7b-0426-4cd4-874b-78eccb99c4fd';

    #appSecret 生产需替换
    const appSecret         = '45O9E7';

    #appPrivateKey 私钥
    const appPrivateKey     ='d0783f2b7e9885ffb8a8268d06f44c6eddc5162d404dd1b37cae63d1b62972d7';

    #publicKey 公钥
    const publicKey         ='0480E866C61C385C2916901CFC10AA53DFD89DBFFA87A4A49C26F82C66F30B7A7038D61FDBE08236F6ACFA6216A6FBCC511841586ABE24107438E049BE98C4660C

';

    #body明文加密方式
    const encrypt           = false;

    #header加密方式
    const Sm2               = 'SM2';

    #header加密方式
    const grantType         = 'client_credentials';

    #见证宝Api
    public function Ejzb_Api($type,$post,$sign){
        $data= $post;
        switch($type)
        {
            case '6000':#会员子账号开户【6000】
                $header     = $this->SYS_HEAD($sign);
                $jsonStr  = json_encode($data,JSON_UNESCAPED_UNICODE);
                $result  = $this->CURL(self::baseUrl.'/V1.0/T0RealAmtQuery',$jsonStr,$isPost=true,$header);
                return $result;
                break;

            default:
                $url ='';
        }
    }

    #获取header头带参数信息头部公共参数--(动态获取)
    private function SYS_HEAD($sign)
    {

        $AccessToken        =$this->GetToken();
        $headers[]          ='Authorization:'.$AccessToken;
        $headers[]          ='x-pab-signature:'.$sign;
        $headers[]          ='x-pab-appID:'.self::appid;
        $headers[]          ="x-pab-global-seqno:12345678".time();
        $headers[]          ='x-pab-timestamp:'.date("Y-m-d H:i:s");
        $headers[]          ='x-pab-signMethod:'.self::Sm2;
        $headers[]          ='x-pab-encrypt:false';;
        $headers[]          ='Accepte:txt/plain';
        $headers[]          ='User-Agent:aop-sdk-php';
        $headers[]          ='Content-Type:application/json';
        #print_r($headers);die;
        return $headers;
    }

    #获取header头带参数信息头部公共参数(固定参数)
    private function SYS_HEAD1()
    {
        $headers[]          ='Authorization:a94cc4c5-bde8-4381-83eb-58249624d5df';
        $headers[]          ='x-pab-signature:eyJraWQiOiJkNWY4ZWU3Yi0wNDI2LTRjZDQtODc0Yi03OGVjY2I5OWM0ZmQiLCJ0eXAiOiJKV1QiLCJhbGciOiJTTTIifQ.e30.MzA0NTAyMjEwMDk2Zjg5NGU2MmQ1N2YwMDRjZjkxY2YwYmM0OGVmOGVhNTgzNzgzMWJjYmQyZmY4YWIwNzFhMmU2MTMyNTMwYTYwMjIwMzUzNmYxOGRlMDI1ZmUzOTgzMWZhMjE1NmY2MTgwOTAwNjJjZDdlOWNjMTg5NjJhMWUzOWZjNjgwNTRhMGViMA';
        $headers[]          ='x-pab-appID:'.self::appid;;
        $headers[]          ='x-pab-global-seqno:1366318537884160000';
        $headers[]          ='x-pab-timestamp:2021-03-01 17:24:59';
        $headers[]          ='x-pab-signMethod:'.self::Sm2;//'x-pab-signMethod:SM2';
        $headers[]          ='x-pab-encrypt:false';
        $headers[]          ='Accepte:txt/plain';
        $headers[]          ='User-Agent:aop-sdk-php';
        $headers[]          ='Content-Type:application/json';
        #print_r($headers);die;
        return $headers ;
    }

    #API交易接口首先要获取Authorization即token字段
    public function GetToken()
    {
        $Authorization['grantType']     = self::grantType;
        $Authorization['scope']         = null;
        $Authorization['clientId']      = self::appid;
        $Authorization['clientSecret']  = self::appSecret;
        $jsonStr  = json_encode($Authorization);
        //发起请求
        $data     =$this->CURL(self::token,$jsonStr,$isPost=true,null);
//        print_r($data);die;
        $response=json_decode($data, true);
        if ($response['Code']!='000000') {
            $this->debug('验证token失败，请重新发起验证',null);
            echo 'errorCode：'.$response['Code']. '<br/>'."msg：".$response['Message'];die;
        }

        return $response['content']['result']['access_token'];

    }


    #获取6位随机数字
    public  function get_random($len = 8)
    {
        $numbers = '0123449489';
        $random = "";
        for ($i = 0; $i < $len; $i++) {
            $random .= $numbers{mt_rand(0, 9)};
        }
        return $random;
    }

    #使用post的curl请求+header头部带参数传输请求
    public function CURL($url,$requestStr,$isPost=true,$headers){
        if (empty($headers)){
            $headers[]          ='Content-Type:application/json';
        }

        $ch = curl_init($url);
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HEADER, 0); //定义是否显示状态头 1：显示 ； 0：不显示
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$requestStr);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_POST, $isPost);
        $response = curl_exec($ch);
        $resinfo = curl_getinfo($ch);
        curl_close($ch);
        if($resinfo["http_code"] != 200){
            $this->debug('请求失败http状态码:',$resinfo["http_code"]);
        }
            $this->debug('请求成功http数据:',json_encode($resinfo));
            $this->debug('请求成功返回值:',$response);
        return $response;
    }


    #日志记录
    protected function debug($tempType,$tempStr){
        $log_name = './src/test/Log/log.txt';
//        $tempStr=date('Y-m-d H:i:s').' '.$tempType."\r\n".$tempStr."\r\n\r\n";
//        $myfile = fopen($log_name, "a");
//        fwrite($myfile, $tempStr);
//        fclose($myfile);
    }




}

